package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends BaseServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Override
    public ResponseResult list(SensitiveDto dto) {
        if (dto==null){
            return paramInvalid;
        }
        dto.checkParam();
        //根据名称模糊查询
        Page p = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNoneBlank(dto.getName())){
            lambdaQueryWrapper.like(AdSensitive::getSensitives,dto.getName());
        }
       IPage resul = page(p,lambdaQueryWrapper);
        //3.结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)resul.getTotal());
        responseResult.setData(resul.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //参数检查
        if (adSensitive==null)return paramInvalid;
        //保存
        adSensitive.setCreatedTime(new Date());
        save(adSensitive) ;
        return success;
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        if (adSensitive==null||adSensitive.getId()==null) return paramInvalid;
        updateById(adSensitive);
        return success;
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        if (id==null)return paramInvalid;
        AdSensitive sensitive = getById(id);
        if (sensitive==null)return error;
        removeById(id);
        return success;
    }
}
